<template>
  <div>

    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      <!-- <el-checkbox label="复选框 A" />
      <el-checkbox label="复选框 C" /> -->
    </el-checkbox-group>
    <!-- 多选列表 -->
    <div style="margin-top:20px;text-align:right">
      <el-button type="primary" @click="hsubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getUserDetailById, updateRole } from '@/api/user.js'
import { getRoles } from '@/api/setting'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
    // vue是现有结构后有数据，所以要用this.nextTick
    // this.loadRolesById()
  },
  methods: {
    async loadRolesById() {
      const res = await getUserDetailById(this.id)
      console.log('当前用户（id）已有角色', res)
      // res.data.roleIds// ['640a78888b']
      // 数据回填
      this.roleIds = res.data.roleIds
    },
    // 获取全部角色
    async loadRoles() {
      const res = await getRoles()
      console.log('全部的角色是', res)
      this.list = res.data.rows
    },
    // 保存修改
    async hsubmit() {
      const res = await updateRole({ id: this.id, roleIds: this.roleIds })
      console.log('保存修改', res)

      this.$message.success('保存成功')
      this.$emit('on-success')
    },
    closeDialog() {
      // 通知父组件关闭弹层
      this.$emit('close')
    }
  }
}
</script>

<style>
</style>
